package com.example.himalaya.adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class TrackListRvAdapter extends RecyclerView.Adapter<TrackListRvAdapter.ViewHolder> {

    private List<Track> mTracks = new ArrayList<>();

    //格式化日期
    private SimpleDateFormat mUpdateDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //格式化时间
    private SimpleDateFormat mDurationFormat = new SimpleDateFormat("mm:ss");
    private onTrackItemClickListener mOnTrackItemClickListener = null;
    private OnItemLongClickListener mOnItemLongClickListener;


    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTrackOrder;
        private final TextView mTrackTitle;
        private final TextView mTrackPlayCount;
        private final TextView mTrackDuration;
        private final TextView mTrackUpateDate;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mTrackOrder = itemView.findViewById(R.id.track_order);
            mTrackTitle = itemView.findViewById(R.id.track_title);
            mTrackPlayCount = itemView.findViewById(R.id.track_play_count);
            mTrackDuration = itemView.findViewById(R.id.track_duration);
            mTrackUpateDate = itemView.findViewById(R.id.track_update_date);
        }

        public void setDate(Track track) {
            mTrackOrder.setText((int)itemView.getTag()+1+"");
            mTrackTitle.setText(track.getTrackTitle());
            String playCountString = null;
            long playCount = track.getPlayCount();
            if (playCount<10000) {
                playCountString = String.valueOf(playCount);
            } else if (playCount < 100000000) {
                playCountString = (playCount / 10000) + "万";
            } else {
                playCountString = (playCount / 100000000) + "亿";
            }
            mTrackPlayCount.setText(playCountString);
            String durationText = mDurationFormat.format(track.getDuration()*1000);//乘1000，是为了变成毫秒
            mTrackDuration.setText(durationText);
            String updateDateText = mUpdateDateFormat.format(track.getUpdatedAt());
            mTrackUpateDate.setText(updateDateText);
        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_track, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        holder.setDate(mTracks.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnTrackItemClickListener != null) {
                    mOnTrackItemClickListener.OnTrackItemClick(mTracks,(int)v.getTag());
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemLongClickListener != null) {
                    mOnItemLongClickListener.onItemLongClick(mTracks.get((int) v.getTag()));
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mTracks != null) {
            return mTracks.size();
        }
        return 0;
    }

    /**
     * 设置曲目数据
     *
     * @param tracks 曲目列表
     */
    @SuppressLint("NotifyDataSetChanged")
    public void setTrackData(List<Track> tracks) {
        if (tracks != null) {
            mTracks.clear();
            mTracks.addAll(tracks);
        }
        notifyDataSetChanged();
    }


    public void setOnTrackItemClickListener(onTrackItemClickListener listener) {
        mOnTrackItemClickListener = listener;
    }

    public interface onTrackItemClickListener {
        /**
         * 曲目被点击
         * @param tracks 当前曲目列表
         * @param position 该曲目在列表中的位置
         */
        void OnTrackItemClick(List<Track> tracks, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener){
        this.mOnItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemLongClickListener{
        void onItemLongClick(Track track);
    }


}
